package cn.jinjimi.app.main.widget.XRecyclerView;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import cn.jinjimi.app.R;

/**
 * Created by lforxeverc on 2017/6/21.
 */

public class PinnedLayout extends FrameLayout {
    private static final int TAG_KEY = 0x01;
    private RecyclerView mRecycleView;
    private boolean showingPinned;
    private View mPinnedView;
    private int pinnedPosition = -1;
    private RecyclerView.OnScrollListener mScrollerListener;
    private IPinnedListener mPinnedListener;

    public PinnedLayout(@NonNull Context context) {
        this(context, null);
    }

    public PinnedLayout(@NonNull Context context, @Nullable AttributeSet attrs) {

        this(context, attrs, 0);
    }

    public PinnedLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public PinnedLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setPinnedListener(IPinnedListener mPinnedListener) {
        this.mPinnedListener = mPinnedListener;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mRecycleView = (RecyclerView) findViewById(R.id.id_xrk_pinned_recycleview);
        mPinnedView = findViewById(R.id.id_xrk_pinned_view);
        mPinnedView.setVisibility(GONE);
        initScroll();
    }

    public void initScroll() {
        mRecycleView.addOnScrollListener(mScrollerListener = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                RecyclerView.LayoutManager llm = recyclerView.getLayoutManager();
                if (llm instanceof LinearLayoutManager) {
                    int firstVisibleItemPosition = ((LinearLayoutManager) llm).findFirstVisibleItemPosition();
                    View v = llm.findViewByPosition(firstVisibleItemPosition);
                    if (v != null) {
                        Object tag = v.getTag();
                        if (tag instanceof PinnedTagView) {
                            mPinnedView.setVisibility(VISIBLE);
                            showingPinned = true;
                            pinnedPosition = firstVisibleItemPosition;
                            if (mPinnedListener != null) {
                                mPinnedListener.onPinnedChange(true);
                            }
                        } else {
                            if (pinnedPosition != -1 && firstVisibleItemPosition > pinnedPosition) {
                                mPinnedView.setVisibility(VISIBLE);
                                showingPinned = true;
                                if (mPinnedListener != null) {
                                    mPinnedListener.onPinnedChange(true);
                                }

                            } else {
                                showingPinned = false;
                                mPinnedView.setVisibility(GONE);
                                if (mPinnedListener != null) {
                                    mPinnedListener.onPinnedChange(false);
                                }
                            }
                        }
                    } else {
                        showingPinned = false;
                        mPinnedView.setVisibility(GONE);
                        if (mPinnedListener != null) {
                            mPinnedListener.onPinnedChange(false);
                        }
                    }
                }
            }
        });

    }

    public void onScroll() {
        if (mScrollerListener != null)
            mScrollerListener.onScrolled(mRecycleView, 0, 1);
    }

    public static void addPinnedTag(View v, int position) {
        if (v != null) {
            v.setTag(new PinnedTagView(position, v));
        }
    }

    public static void clearPinnedTag(View v, int position) {
        if (v != null) {
            v.setTag(null);
        }
    }

    public interface IPinnedListener {
        void onPinnedChange(boolean pinned);
    }

}
